package com.kingyun.gpsinspection.purificationservice.services.jsm.outerservice.impl;

import com.alibaba.fastjson.JSONObject;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.EntityPro;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.IntDataSrcItemValue;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.OutIntDataSrcItemValueService;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.property.JsmEntityPropertyService;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.reportTemp.JsmReportService;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by huangxueqian on 2017/4/6/006.
 */
public class OutIntDataSrcItemValueServiceImpl implements OutIntDataSrcItemValueService {
    @Autowired
    private JsmReportService reportService ;

    @Autowired
    private JsmEntityPropertyService entityPropertyService ;
    @Override
    public IntDataSrcItemValue getLastIntDataSrcItemValue(IntDataSrcItemValue intDataSrcItemValue) throws Exception {
        return reportService.getLastIntDataSrcItemValue(intDataSrcItemValue);
    }

    @Override
    public List<IntDataSrcItemValue> getInDataSrcItemValue(IntDataSrcItemValue intDataSrcItemValue) throws Exception {
        return reportService.selectIntDataSrcItemList(intDataSrcItemValue);
    }

    /**
     * 根据特性值传递数据展现出问题
     * @param propertyId , 查询站上面的问题
     */
    @Override
    public JSONObject getInDataSrcItemValueJson(String  propertyId) throws Exception {
                    if(propertyId==null){
                        propertyId = "0411162621095000";
                    }
                List<String> xList = new ArrayList<>();
                List<Object> yList = new ArrayList<>();
                JSONObject jsonObject = new JSONObject();
                EntityPro entityPro =  entityPropertyService.selectByPrimaryKey(propertyId);
                        /**
                         *  查询场站的数据直接 出现问题
                         */
                JSONObject  proJson = new JSONObject();
                proJson.put("name",entityPro.getPropertyName());
                IntDataSrcItemValue itemValue = new IntDataSrcItemValue();
                itemValue.setDataSrcItemId(entityPro.getDevicePropertyId());
                List<IntDataSrcItemValue> intDataSrcItemValueList =  reportService.selectIntDataSrcItemList(itemValue) ;
                        if(intDataSrcItemValueList!=null && intDataSrcItemValueList.size() > 0){
                            List<Double> doubleList  =  new ArrayList<>();
                            for(IntDataSrcItemValue itemValueD : intDataSrcItemValueList){
                                    xList.add(itemValueD.getTimestr());
                                    double   ivs   =   Double.parseDouble(itemValueD.getDataSrcItemValue());
                                    BigDecimal bValues = new BigDecimal(ivs);
                                    double   formatters   =   bValues.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();
                                    doubleList.add(formatters);
                            }
                            proJson.put("data", doubleList);
                        }
                            yList.add(proJson);
                            jsonObject.put("x",xList);
                            jsonObject.put("y",yList);
                            List<Object> object = new ArrayList<>();
                            JSONObject A = new JSONObject();
                            JSONObject B = new JSONObject();
                            JSONObject C = new JSONObject();
                            JSONObject D = new JSONObject();
                            JSONObject F = new JSONObject();
                            JSONObject E = new JSONObject();
                            JSONObject G = new JSONObject();
                            JSONObject J = new JSONObject();
                            JSONObject Q = new JSONObject();
                            A.put("id","0411162621095000");
                            A.put("name","1机机组转速");
                            B.put("id","0411162621093000");
                            B.put("name","1机进气压力");
                            C.put("id","0411162621091000");
                            C.put("name","1机燃气进机压力");
                            D.put("id","0411162621081000");
                            D.put("name","2机机组转速");
                            F.put("id","0411162621080000");
                            F.put("name","2机进气压力");
                            E.put("id","0411162621077000");
                            E.put("name","2机燃气进机压力");
                            G.put("id","0411162621061000");
                            G.put("name","3机机组转速");
                            J.put("id","0411162621060000");
                            J.put("name","3机进气压力");
                            Q.put("id","0411162621056000");
                            Q.put("name","3机压缩1缸排压");
                            object.add(A);
                            object.add(B);
                            object.add(C);
                            object.add(D);
                            object.add(F);
                            object.add(E);
                            object.add(G);
                            object.add(J);
                            object.add(Q);
                            jsonObject.put("stations",object);
        return jsonObject;
    }
}
